using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PackageLocalData
{
    private PackageLocalData()
    {
    }

    private static PackageLocalData instance;

    public static PackageLocalData Instance
    {
        get
        {
            if (instance == null)
            {
                instance = new PackageLocalData();
            }

            return instance;
        }
    }

    public List<PackageLocalItem> tableItems;

    public void SaveData()
    {
        string json = JsonUtility.ToJson(this);
        PlayerPrefs.SetString("PackageLocalData", json);
        PlayerPrefs.Save();
    }

    public List<PackageLocalItem> LoadData()
    {
        if (tableItems != null)
        {
            return tableItems;
        }

        if (PlayerPrefs.HasKey("PackageLocalData"))
        {
            string str = PlayerPrefs.GetString("PackageLocalData");
            PackageLocalData packageLocalData = JsonUtility.FromJson<PackageLocalData>(str);
            tableItems = packageLocalData.tableItems;
        }
        else
        {
            tableItems = new List<PackageLocalItem>();
        }

        return tableItems;
    }

    public void RemoveData(PackageLocalItem localItem)
    {
        tableItems.Remove(localItem);
    }
}


/// <summary>
/// 动态数据，徐保存
/// </summary>
[System.Serializable]
public class PackageLocalItem
{
    public string uid;
    public int id;
    public int num;
    public int level;
    public bool isNew;

    public override string ToString()
    {
        return
            $"{nameof(uid)}: {uid}, {nameof(id)}: {id}, {nameof(num)}: {num}, {nameof(level)}: {level}, {nameof(isNew)}: {isNew}";
    }
}